package com.Mobile.Action;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.Action.MediaAction;
import com.Entity.Favouritevideo;
import com.Entity.Media;
import com.Entity.Mediacomment;
import com.Service.FavouriteVideoService;
import com.Service.MediaCommentService;
import com.Service.MediaService;

public class MobileMediaAction {
	
	private static final Logger logger = Logger.getLogger(MobileMediaAction.class);

	private Media myMedia=new Media();
	@Autowired
	private MediaService mediaservice;
	private List<Media> medialist=new ArrayList<Media>();
	private List<Media> hosmedialist;

	private Mediacomment mediacomment=new Mediacomment();
	@Autowired
	private MediaCommentService mediacommentservice;
	private List<Mediacomment> mediacommentlist;
	private Favouritevideo favouritevideo=new Favouritevideo();
	@Autowired
	private FavouriteVideoService favouriteVideoService;
	private List<Favouritevideo> favouritevideolist;
	private List<Long> numberlist=new ArrayList<Long>();
	HttpServletResponse response = ServletActionContext.getResponse();
	JSONObject returnjson=new JSONObject();		
	
	private int flag=0;

	
	private Map request = null;
	
	private Media media=new Media();

	public void listHosVideos() throws Exception {
		hosmedialist=this.mediaservice.findHosVideos();
		JSONObject returnjson=new JSONObject();		
		response.setContentType("application/json;charset=utf-8"); 
		PrintWriter out = response.getWriter();	
		if(null!=hosmedialist&&!hosmedialist.isEmpty()){
			for(int i=0;i<hosmedialist.size();i++){
				long favouritenumber=this.favouriteVideoService.countFavouriteNumberByVideoId(hosmedialist.get(i).getId());
				logger.info("favouritenumber:"+favouritenumber);
				this.numberlist.add(favouritenumber);
			}
			JSONArray jsonbean=JSONArray.fromObject(hosmedialist);
			flag=1;
			returnjson.put("flag", flag);
			returnjson.put("info", jsonbean);	
			JSONArray favouriteNumber=JSONArray.fromObject(numberlist);
			returnjson.put("favouriteNumber", favouriteNumber);	
		}
		else{
			returnjson.put("flag", flag);
		}
		out.println(returnjson.toString());
		out.flush();
		out.close();	
	}

	
	public void collectVideo() throws Exception {
		String videoid=ServletActionContext.getRequest().getParameter("videoid");
		videoid =new String(videoid.getBytes("ISO-8859-1"), "UTF-8");
		logger.info(videoid);
		String userid=ServletActionContext.getRequest().getParameter("userid");
		userid =new String(userid.getBytes("ISO-8859-1"), "UTF-8");
		response.setContentType("application/json;charset=utf-8"); 
		PrintWriter out = response.getWriter();	
		logger.info(userid);
		if(null==videoid||videoid.isEmpty()||null==userid||userid.isEmpty()){
			logger.info("error");
			returnjson.put("flag", flag);
		}
		else{
			favouritevideolist=this.favouriteVideoService.findCollectedVideo(Integer.parseInt(videoid), Integer.parseInt(userid));
			if(null!=favouritevideolist&&!favouritevideolist.isEmpty()){
				flag=2;
				returnjson.put("flag", flag);
			}
			else{
				favouritevideo.setVideoId(Integer.parseInt(videoid));
				favouritevideo.setUserId(Integer.parseInt(userid));
				favouritevideo.setFavouriteTime(new Date());
				this.favouriteVideoService.save(favouritevideo);
				flag=1;
				returnjson.put("flag", flag);
			}
		}
		out.println(returnjson.toString());
		out.flush();
		out.close();
	}
	
	public void listFavouriteVideo() throws Exception {
		String userid=ServletActionContext.getRequest().getParameter("userid");
		userid =new String(userid.getBytes("ISO-8859-1"), "UTF-8");
		favouritevideolist=this.favouriteVideoService.findFavouriteVideoByUserId(Integer.parseInt(userid));
		JSONObject returnjson=new JSONObject();		
		response.setContentType("application/json;charset=utf-8"); 
		PrintWriter out = response.getWriter();	
		if(null!=favouritevideolist&&!favouritevideolist.isEmpty()){
			for(int i=0;i<favouritevideolist.size();i++){
				myMedia=this.mediaservice.getById(favouritevideolist.get(i).getVideoId());
				long favouritenumber=this.favouriteVideoService.countFavouriteNumberByVideoId(favouritevideolist.get(i).getVideoId());
				logger.info("favouritenumber:"+favouritenumber);
				this.medialist.add(myMedia);
				this.numberlist.add(favouritenumber);

			}
			
			JSONArray jsonbean=JSONArray.fromObject(medialist);
			JSONArray favouriteNumber=JSONArray.fromObject(numberlist);
			flag=1;
			returnjson.put("flag", flag);
			returnjson.put("info", jsonbean);	
			returnjson.put("favouriteNumber", favouriteNumber);	
		}
		else{
			returnjson.put("flag", flag);
		}
		out.println(returnjson.toString());
		out.flush();
		out.close();	
	}

}
